package dymnd.ui;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.LayoutManager;

import javax.swing.JComponent;

public class WorkspaceLayoutManager	implements LayoutManager	{
	public static final String COMPONENT_UTILITY_MENU = "utility menu";
	public static final String COMPONENT_CANVAS = "canvas";
	public static final String COMPONENT_STATUS_BAR_LOWER = "lower status bar";
	
	JComponent canvas = null;
	PluginOptionPanel utilityMenu = null;
	JComponent lowerStatusBar = null;
	
	Container container = null;
	
	public WorkspaceLayoutManager(Container c)	{
		container = c;
	}
	
	public void layoutContainer(Container c)	{
		if(container == null)
			container = c;
		
		
		
		int utilityMenuHeight = 50;
		//for(int i = 0; i < components.length; i++)	{
			if(utilityMenu != null)	{
				utilityMenu.setSize(c.getWidth(), utilityMenuHeight);
				utilityMenu.setLocation(0, 0);
				//if(utilityMenu.getComponentCount() > 0)
					//utilityMenu.paintComponents(utilityMenu.getGraphics());
			}
			if(canvas != null)	{
				
				int y = ((c.getHeight() - utilityMenuHeight) / 2) - (canvas.getHeight() / 2);
				if(y < utilityMenuHeight)
					y = utilityMenuHeight + 1;
				canvas.setLocation(((c.getWidth() / 2) - (canvas.getWidth() / 2)), y);
				
				
				
				
					
				
				int canvasWidth = 0;
				int canvasHeight = 0;
				if(canvas.getPreferredSize().width < c.getWidth())
					canvasWidth = canvas.getPreferredSize().width;
				else
					canvasWidth = c.getWidth();
				
				if(canvas.getPreferredSize().height < c.getHeight() - 50)
					canvasHeight = canvas.getPreferredSize().height;
				else
					canvasHeight = c.getHeight() - 50;
					
				canvas.setSize(canvasWidth, canvasHeight);

				
				
			}
			//System.out.println("Width: " + c.getWidth() + " Height: " + c.getHeight());
		}
	
	
		
	
	
	public void addComponent(JComponent c, String s)	{
		if(s.equals(COMPONENT_UTILITY_MENU))	{
			utilityMenu =(PluginOptionPanel) c;
			container.add(utilityMenu);
		}
		else if(s.equals(COMPONENT_CANVAS))	{
			canvas = (JComponent) c;
			container.add(c);
		}
		else if(s.equals(COMPONENT_STATUS_BAR_LOWER))	{
			lowerStatusBar = (JComponent) c;
			container.add(lowerStatusBar);
		}
		else 
			throw new IllegalArgumentException();
	}
	
	public void addLayoutComponent(String s, Component c)	{
		if(s.equals(COMPONENT_UTILITY_MENU))	{
			utilityMenu =(PluginOptionPanel) c;
			container.add(utilityMenu);
		}
		else if(s.equals(COMPONENT_CANVAS))	{
			canvas = (JComponent) c;
			container.add(canvas);
		}
		else if(s.equals(COMPONENT_STATUS_BAR_LOWER))	{
			lowerStatusBar = (JComponent) c;
			container.add(lowerStatusBar);
		}
		else 
			container.add(c);
	}
	
	public Dimension preferredLayoutSize(Container c)	{
		return c.getSize();
	}
	public Dimension minimumLayoutSize(Container c)	{
		return c.getSize();
	}
	public void removeLayoutComponent(Component c)	{
		
	}
	
}